/**
 * File		: Camera.java
 * Subject	: Represents the camera in the world
 * Author	: Isaac Parenteau (locus2k@gmail.com)
 * Date		: April 16th, 2011
 */
package src.project.util;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

public class Camera {

	public Vector3 Eye;
	public Vector3 Reference;
	public Vector3 Up;

	private GL10 gl;
	private float far;
	private float near;
	private float top;
	private float bottom;
	private float left;
	private float right;
	
	/**
	 * Default Constructor
	 * @param gl 
	 */
	public Camera(GL10 gl){
		this(gl, -1f, 1f, 1f, -1f, 2f, 10f, Vector3.Zero, Vector3.Zero, Vector3.Up);
	}
	
	/**
	 * Constructor
	 * @param gl opengl es
	 * @param left screen left
	 * @param right screen right
	 * @param top screen top
	 * @param bottom screen bottom
	 * @param near non-negative near plane
	 * @param far non-negative far plane
	 * @param eye coordinates of where the camera resides
	 * @param reference where the camera points to
	 * @param up Up vector
	 */
	public Camera(GL10 gl, float left, float right, float top, float bottom,
			float near, float far, Vector3 eye, 
			Vector3 reference, Vector3 up){
		Eye = eye;
		Reference = reference;
		Up = up;
		
		this.gl = gl;
		this.far = far;
		this.near = near;
		this.left = left;
		this.right = right;
		this.bottom = bottom;
		this.top = top;
	}
	
	/**
	 * Creates the camera
	 */
	public void createCamera(){
		createLookAt();
		createPerspective();
	}
	
	/**
	 * Generates the look at
	 */
	public void createLookAt(){
        gl.glMatrixMode (GL10.GL_MODELVIEW);
        gl.glLoadIdentity ();
        GLU.gluLookAt(gl, Eye.X, Eye.Y, Eye.Z, Reference.X, Reference.Y, Reference.Z, 
        		Up.X, Up.Y, Up.Z);
	}
	
	/**
	 * Generatees the perspective
	 */
	public void createPerspective(){
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(left, right, bottom, top, near, far);
	}
}
